package com.system.run.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.run.entity.Nurse;
import com.system.run.entity.Patient;
import com.system.run.entity.vo.NurseVo;
import com.system.run.entity.vo.PatientVo;
import com.system.run.from.Row;
import com.system.run.service.NurseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import sun.security.krb5.internal.crypto.NullEType;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-01-08
 */
@RestController
@RequestMapping("/nurse")
public class NurseController {

    @Autowired
    public NurseService nurseService;

    @ApiOperation("依据ID查询护士详情")
    @PostMapping("getNurse/{id}")
    public Row GetNurse(@ApiParam(name = "id", value = "护士标识",required = false) @PathVariable String id){
        QueryWrapper<Nurse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Nurse nurse = nurseService.getOne(queryWrapper);
        return Row.SUCCESS().data("nurse",nurse);
    }

    @ApiOperation("保存护士详情")
    @PostMapping("saveNurse")
    public Row SaveNurse( @ApiParam(name = "nurse", value = "护士",required = false) @RequestBody Nurse nurse) {
        boolean flag = true;
        if (!StringUtils.isEmpty(nurse.getId())){
            flag = nurseService.updateById(nurse);
        }else{
            flag = nurseService.save(nurse);
        }
        if (flag){
            return Row.SUCCESS();
        }else{
            return Row.ERROR();
        }
    }

    @ApiOperation("保存护士详情")
    @PostMapping("removeNurse/{id}")
    public Row RemoveNurse(@ApiParam(name = "id", value = "护士标识",required = false) @PathVariable String id) {
        QueryWrapper<Nurse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        boolean flag = nurseService.remove(queryWrapper);
        if (flag){
            return Row.SUCCESS();
        }else{
            return Row.ERROR();
        }
    }

    @ApiOperation("获取所有护士详情")
    @PostMapping("getNurseList/{current}/{limit}")
    public Row GetNurseList( @ApiParam(name = "current", value = "当前页",required = false) @PathVariable Long current,
                               @ApiParam(name = "limit", value = "每页记录数",required = false) @PathVariable Long limit,
                               @RequestBody(required = false) NurseVo nurseVo
    ) {
        QueryWrapper<Nurse> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(nurseVo.getName())){
            queryWrapper.like("name",nurseVo.getName());
        }
        if (!StringUtils.isEmpty(nurseVo.getSex())){
            queryWrapper.eq("sex",nurseVo.getSex());
        }
        if (!StringUtils.isEmpty(nurseVo.getAge())){
            queryWrapper.eq("age",nurseVo.getAge());
        }
        if (!StringUtils.isEmpty(nurseVo.getPhone())){
            queryWrapper.like("phone",nurseVo.getPhone());
        }
        Page<Nurse> nursePage = new Page<>(current,limit);
        nurseService.page(nursePage,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",nursePage.getTotal());
        map.put("records",nursePage.getRecords());
        return Row.SUCCESS().data(map);
    }
}

